<?php


namespace we;

use think\Db;
use think\Config;
use think\Session;
use think\Request;

// 处理成兼容微擎的版本
class WeEngine
{
    // 数据库(首次执行)
    public function uniacidSql()
    {
        $database = config('database.database');
        $sql = "SELECT CONCAT('ALTER TABLE `', table_name, '` ADD COLUMN `uniacid` INT(11) NOT NULL DEFAULT 0 COMMENT \"应用id\"') as query FROM information_schema.TABLES WHERE table_schema = '". $database ."'";

        $list = Db::query($sql);
        $list[] = [
            'query' => "CREATE TABLE `ims_ymq_changdi_addon_value` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `uniacid` int(11) NOT NULL DEFAULT '0',
  `name` varchar(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `value` text COLLATE utf8_unicode_ci,
  `createtime` int(11) NOT NULL DEFAULT '0',
  `updatetime` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
        ];
        foreach ($list as $item){
            try{
                Db::query($item['query']);
            }catch (\Exception $e){

            }
        }

    }

    public function configSql()
    {
        $uniacid = config('site.uniacid');
        $config_list = Db::query('select * from '. config('database.prefix') .'config where uniacid=0');

        foreach ($config_list as $key=>$item){
            $res = Db::name('config')->where('name', $item['name'])->find();
            if(!$res) {
                unset($item['id']);
                $item['uniacid'] = $uniacid;
                Db::name('config')->insert($item);
            }
        }
    }

}
